package com.cooper.controllers.dev;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.controllers.Template;
import com.cooper.core.JsonResult;
import com.cooper.service.core.JedisService;
import com.cooper.vo.RedisVo;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.portal.Portal;
import redis.clients.jedis.ShardedJedis;

public class RedisController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private JedisService jedisService;
	
	@Template(title="Redis")
	@Get
	public String execute(Invocation inv, Portal portal){
		return "Redis";
	}
	
	@Get("get") @Post("get")
	public Object get(Invocation inv, RedisVo vo){
		JsonResult json = new JsonResult();
		json.setSuccess(false);
		try{
			ShardedJedis jedis = this.jedisService.getJedis();
			String retValue = jedis.get(vo.getKey());
			this.jedisService.returnRes(jedis);
			json.setSuccess(true);
			json.setMessage(retValue);
		} catch(Exception e){
			json.setMessage("error:{0}", e);
			logger.error("Error:", e);
		}
		return json;
	}
	
	@Get("delete") @Post("delete")
	public Object delete(Invocation inv, RedisVo vo){
		JsonResult json = new JsonResult();
		json.setSuccess(false);
		try{
			ShardedJedis jedis = this.jedisService.getJedis();
			Long retValue = jedis.del(vo.getKey());
			this.jedisService.returnRes(jedis);
			json.setSuccess(true);
			json.setMessage("return {0}", retValue);
		} catch(Exception e){
			json.setMessage("error:{0}", e);
			logger.error("Error:", e);
		}
		return json;
	}
	
	@Get("set") @Post("set")
	public Object set(Invocation inv, RedisVo vo){
		JsonResult json = new JsonResult();
		json.setSuccess(false);
		try{
			ShardedJedis jedis = this.jedisService.getJedis();
			String retValue = jedis.set(vo.getKey(), vo.getValue());
			this.jedisService.returnRes(jedis);
			json.setSuccess(true);
			json.setMessage(retValue);
		} catch(Exception e){
			json.setMessage(e.getMessage());
			logger.error("Error:", e);
		}
		return json;
	}
	
	@Get("hget") @Post("hget")
	public Object hget(Invocation inv, RedisVo vo){
		JsonResult json = new JsonResult();
		json.setSuccess(false);
		try{
			ShardedJedis jedis = this.jedisService.getJedis();
			String retValue = jedis.hget(vo.getKey(), vo.getField());
			this.jedisService.returnRes(jedis);
			json.setSuccess(true);
			json.setMessage(retValue);
		} catch(Exception e){
			json.setMessage(e.getMessage());
			logger.error("Error:", e);
		}
		return json;
	}
	
	@Get("hset") @Post("hset")
	public Object hset(Invocation inv, RedisVo vo){
		JsonResult json = new JsonResult();
		json.setSuccess(false);
		try{
			ShardedJedis jedis = this.jedisService.getJedis();
			Long retValue = jedis.hset(vo.getKey(), vo.getField(), vo.getValue());
			this.jedisService.returnRes(jedis);
			json.setSuccess(true);
			json.setMessage(retValue.toString());
		} catch(Exception e){
			json.setMessage(e.getMessage());
			logger.error("Error:", e);
		}
		return json;
	}
	
	@Get("hkeys") @Post("hkeys")
	public Object hkeys(Invocation inv, RedisVo vo){
		JsonResult json = new JsonResult();
		json.setSuccess(false);
		try{
			ShardedJedis jedis = this.jedisService.getJedis();
			Set<String> retValue = jedis.hkeys(vo.getKey());
			this.jedisService.returnRes(jedis);
			json.setSuccess(true);
			json.setMessage(retValue.toString());
		} catch(Exception e){
			json.setMessage(e.getMessage());
			logger.error("Error:", e);
		}
		return json;
	}

}
